#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence
@file: send_beanstalk_compare.py
@time: 2018/1/21 13:22
"""

import sys

sys.path.append('..')
sys.path.append('../..')

from common.mongo import MongDb
from common.table_manage import TableManage
from config.mongo_conf import MONGO_DB_SOURCE
from logger import Logger

IMPORT_FILE_DICT = {
    'hunan': 'hunan.txt',
    'gansu': 'gansu.txt',
    'jiangsu': 'jiangsu.txt',
    'shandong': 'shandong.txt',
}
# 日志模块
global_log = Logger('check_compare.log').get_logger()


class CheckCompare(object):
    def __init__(self, _log):
        self.log = _log

        # 初始化mongodb
        self.source_db = MongDb(MONGO_DB_SOURCE['host'], MONGO_DB_SOURCE['port'], MONGO_DB_SOURCE['db'],
                                MONGO_DB_SOURCE['username'],
                                MONGO_DB_SOURCE['password'], log=_log)

    def start(self):

        is_all_finish = True
        for province, file_name in IMPORT_FILE_DICT.iteritems():
            # 种子表名称
            seed_table = TableManage.get_seed_table(province)
            finish_total = 0
            not_find_total = 0
            not_finish_total = 0
            not_find_list = []
            not_finish_list = []
            with open(file_name) as p_file:
                for line in p_file:
                    company = line.strip('\n').strip('\r')
                    item = self.source_db.find_one(seed_table, {'_id': company})
                    if item is None:
                        is_all_finish = False
                        not_finish_list.append(company)
                        not_finish_total += 1
                        continue

                    crawl_status = item.get('crawl_status')
                    if crawl_status == 1:
                        finish_total += 1
                        continue

                    search_status = item.get('search_status')
                    if search_status == 2:
                        not_find_total += 1
                        not_find_list.append(company)
                        continue

                    not_finish_total += 1
                    not_finish_list.append(company)
                    is_all_finish = False
            self.log.info("当前省份完成数目: province = {} finish = {} not find = {} not finish = {}".format(
                province, finish_total, not_find_total, not_finish_total))
            for result in not_find_list:
                self.log.info("未找到企业名单: {} {}".format(province, result))
            for result in not_finish_list:
                self.log.info("未完成企业名单: {} {}".format(province, result))
        if is_all_finish:
            self.log.info("所有企业抓取成功!")


def main():
    CheckCompare(_log=global_log).start()


if __name__ == '__main__':
    main()
